// pages/addressList/addressList.js
var httpUtil = require('../../utils/httpUtil.js');
var util = require('../../utils/util.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag:false,
    userInfo:{},
    addRessList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    that.getList();
  },
 
  getList: function () {
    var that = this;
    var userInfo = util.getKey("userInfo");

    httpUtil.getHttp("/user/address/list?userId=" + userInfo.id, {}, function (res) {
      var flag=false;
   
      if (res == null || res.length==0){
        flag=true;
      }

      that.setData({
        addRessList: res,
        flag: flag,
        userInfo: userInfo
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }, updAddress: function (e) {
    var id = e.target.dataset.id;
    if(id==undefined){
      return;
    }
    wx.navigateTo({
      url: '../addressUpt/addressUpt'
    })
  },
  addAddress: function (e) {
    var id = e.target.dataset.id;
    if (id == undefined) {
      return;
    }
    wx.navigateTo({
      url: '../addressAdd/addressAdd?id='+id
    })
  },
  save: function () {
  
    wx.navigateTo({
      url: '../addressAdd/addressAdd'
    })
  },
   setAddress:function(e){
    var that = this;
     var id = e.target.dataset.id;
     var index = e.target.dataset.index
     httpUtil.postHttp("/user/address/set/"+id+"?userId="+that.data.userInfo.id, {}, function (res) {
      console.log(res);
       that.getList();
    })
  },
  delAddress: function (e) {
    var that = this;
    var id = e.target.dataset.id;
    httpUtil.postHttp("/user/address/del/" + id + "?userId=" + that.data.userInfo.id, {}, function (res) {
      that.getList();
    })
  },ret:function(){
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.getAddress();
    wx.navigateBack()
  }
})